package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baor.dao.*;
import com.baor.domain.ScheduleVo;
import com.baor.domain.TeacherClassVo;
import com.baor.domain.TeacherVo;
import com.baor.domain.WeekVo;
import com.baor.service.ScheduleService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.baor.enums.Results.DELETE_SUCCESS;
import static com.baor.enums.Results.INSERT_SUCCESS;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleDao scheduleDao;

    @Autowired
    private WeekDao weekDao;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private TeacherDao teacherDao;
    
    @Autowired
    private TeacherClassDao teacherClassDao;

    /**
     * 根据班级id，查询班级课程表并返回
     * @param classId
     * @return
     */
    @Override
    public AjaxResult<?> list(Integer classId) {
        List<Map<String,Object>> list = new ArrayList<>();
        List<Integer> weekIds = weekDao.selectList(new LambdaQueryWrapper<WeekVo>()).stream().map( x -> Integer.valueOf(x.getWeekId()) ).collect(Collectors.toList());
        for ( Integer weekId : weekIds ){
            // 通过 班级 星期 推(选择逐个推) 上课科目 上课老师
            //select  from t_schedule where weekId = #{} and classId = #{} ->List<Integer>
            Map<String,Object> map = new HashMap<>();
            map.put("classId",classId);
            map.put("weekId",weekId);
            List<ScheduleVo> scheduleList = scheduleDao.list(map);
            Map<String,Object> res = new HashMap<>();
            res.put("weekId",weekId);
            res.put("subAndTeaList",scheduleList);
            list.add(res);
        }
        return AjaxResult.success(list);
    }

    /**
     * 插入课程表列表
     * @param list
     * @return
     */
    @Override
    public AjaxResult<?> insert(List<ScheduleVo> list) {
        for ( ScheduleVo vo : list )
            scheduleDao.insert(vo);
        return AjaxResult.success(INSERT_SUCCESS);
    }

    /**
     * 在插入课程表之前的检验，验证是否可以添加表内数据 - 只做添加用
     * 入参：整个课程表
     * 出参：可以添加 / 错误的位置
     * @param list
     * @return
     */
    //设置 mybatis-plus null默认值
    @Override
    public AjaxResult<?> check(List<ScheduleVo> list) {
        //主要强调教师，在表中教师数据同时间，班级，数量较少
        //教师一个字段，相对与时间确认两个字段更简单
        // 弱化班级概念,强调时间,教师概念
        //老师：此时间段不可以有课成
        //班级：此时间段不可以有课程
        List<ScheduleVo> res = new ArrayList<>();
        List<String> teacherIdList = list.stream().map( x -> x.getTeacherId() )
                                                    .distinct()
                                                    .collect(Collectors.toList());//教师idList
        for ( String teacherId: teacherIdList ){
            //循环遍历老师们
            //检查每个老师对应时间段内是否有课程
            //查出改老师所有课程
            List<ScheduleVo> newSchedule = list.stream()
                                                .filter( x -> x.getTeacherId().equals(teacherId) )
                                                .collect(Collectors.toList());//该老师此次新添加的课程
            for ( ScheduleVo news : newSchedule ){
                LambdaQueryWrapper<ScheduleVo> teacherQueryWrapper = new LambdaQueryWrapper<>();
                teacherQueryWrapper.eq(ScheduleVo::getWeekId,news.getWeekId())
                                    .eq(ScheduleVo::getCourseId,news.getCourseId())
                                    .eq(ScheduleVo::getTeacherId,news.getTeacherId());
                List<ScheduleVo> scheduleVos = scheduleDao.selectList(teacherQueryWrapper);//重复
                res.addAll(scheduleVos);
            }
        }
        if ( res.size() == 0 )
            return AjaxResult.success("可以进行添加");
        List<Integer> schedules = res.stream().map(x -> Integer.valueOf(x.getScheduleId()))
                                                .collect(Collectors.toList());
        List<ScheduleVo> scheduleVos = scheduleDao.list2(schedules);
        return AjaxResult.error("如下重复",scheduleVos);
    }

    /**
     * 传入List<ScheduleVo>修改
     * 根据 scheduleId 修改
     * @param list
     * @return
     */
    @Override
    public AjaxResult<?> update(List<ScheduleVo> list) {
        for ( ScheduleVo vo : list )
            scheduleDao.updateById(vo);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult<?> week() {
        return AjaxResult.success(weekDao.selectList(null));
    }

    @Override
    public AjaxResult<?> course() {
        return AjaxResult.success(courseDao.selectList(null));
    }

    @Override
    public AjaxResult<?> subjectId(Integer subjectId) {
        List<TeacherClassVo> teacherClassVos = teacherClassDao.selectList(new LambdaQueryWrapper<TeacherClassVo>().eq(TeacherClassVo::getSubjectId, subjectId));
        List<TeacherVo> teacherVos = teacherDao.selectList(new LambdaQueryWrapper<TeacherVo>().in(TeacherVo::getTeacherId, teacherClassVos.stream().map(x -> x.getTeacherId()).collect(Collectors.toList())));
//        List<TeacherVo> teacherVos = teacherDao.selectList(new LambdaQueryWrapper<TeacherVo>().eq(TeacherVo::getSubjectId, subjectId));
        return AjaxResult.success(teacherVos);
    }

    @Override
    public AjaxResult<?> deleteT(TeacherClassVo teacherClassVo) {
        System.out.println(teacherClassVo);
        int delete = teacherClassDao.delete(new LambdaQueryWrapper<TeacherClassVo>().eq(TeacherClassVo::getSubjectId, teacherClassVo.getSubjectId()).eq(TeacherClassVo::getTeacherId, teacherClassVo.getTeacherId()));
        return AjaxResult.success(DELETE_SUCCESS);
    }

}
